package com.qf.sys.util;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @创建人 sunsir Email:sunsir163@163.com
 * @创建时间 2022/5/10
 * @描述
 */
public class Filter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //return HandlerInterceptor.super.preHandle(request, response, handler);
        // 在拦截点执⾏前拦截，如果返回true则不执⾏拦截点后的操作（拦截成功）
        // 返回false则不执⾏拦截
        System.out.println("HandlerInterceptor->preHandle");
        HttpSession session = request.getSession();
        //String uri = request.getRequestURI(); // 获取登录的uri，这个是不进⾏拦截的
        //if(session.getAttribute("LOGIN_USER")!=null || uri.indexOf("system/login")!=-1) {// 说明登录成功 或者 执⾏登录功能
        if (session.getAttribute("loginUser") != null) {
            // 登录成功不拦截
            return true;
        } else {
            // 拦截后进⼊登录页⾯
            response.sendRedirect("index.jsp");
            return false;
        }
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("HandlerInterceptor->postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("HandlerInterceptor->afterCompletion");

    }
}
